﻿#include"snake.h"

void SetPos(short x, short y)
{
	//获得标准输出设备的句柄
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);

	//定位光标位置
	COORD pos = { x,y };
	SetConsoleCursorPosition(houtput, pos);
}

void WelcomeToGame()
{
	SetPos(40, 14);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	SetPos(42, 20);
	system("pause");
	system("cls");
	SetPos(40, 14);
	wprintf(L"用→←↑↓来控制蛇的移动，按F3加速，F4减速");
	SetPos(40, 15);
	wprintf(L"加速能够获得更高的分数");
	SetPos(42, 20);
	system("pause");
	system("cls");
}
//创建地图
void CreateMap()
{
	//上 下 左右
	for (int i = 0; i < 58; i++)
	{
		wprintf(L"%lc", WALL);
	}
	SetPos(0, 26);
	for (int i = 0; i < 58; i++)
	{
		wprintf(L"%lc", WALL);
	}
	for (int i = 1; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	for (int i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}

//初始化蛇身
void InitSnake(pSnake ps)
{
	int i = 0;
	pSnakeNode cur = NULL;
	for (i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (NULL == cur)
		{
			perror("InitSnake()::malloc()");
			exit(1);
		}
		cur->next = NULL;
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;

		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}

	//设置贪吃蛇的属性
	ps->_dir = RIGHT;
	ps->_score = 0;
	ps->_food_weight = 10;
	ps->_sleep_time = 200;//(ms)
	ps->_status = OK;

}
//创建食物
void CreateFood(pSnake ps)
{
	int x, y;
	x = y = 1;
again:
	do {
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x%2 != 0);

	//x,y 不与蛇冲突
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (x == cur->x && y == cur->y)
		{
			goto again;
		}
		cur = cur->next;
	}

	//创建食物的结点
	pSnakeNode pFood = (pSnakeNode*)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreateFood()::malloc()");
		exit(1);
	}
	pFood->x = x;
	pFood->y = y;
	pFood->next = NULL;
	SetPos(x, y);
	wprintf(L"%lc", FOOD);
	ps->_pFood = pFood;
}

void GameStart(pSnake ps)
{
	//0.设置窗口大小并隐藏光标
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);

	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(houtput, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(houtput, &CursorInfo);
	//1.打印环境界面
	//2.功能介绍
	WelcomeToGame();
	//3.绘制地图
	CreateMap();
	//4.创建蛇
	InitSnake(ps);
	//5.食物
	CreateFood(ps);
}

//运行游戏
void PrintHelpInfo()
{
	SetPos(64, 10);
	wprintf(L"%ls",L"不能穿墙，不能咬到自己");
	SetPos(64, 11);
	wprintf(L"%ls",L"用→←↑↓来控制蛇的移动，按F3加速，F4减速");
	SetPos(64, 12);
	wprintf(L"%ls",L"加速能够获得更高的分数");
	SetPos(64, 13);
	wprintf(L"%ls",L"exit 退出，space 暂停");
	SetPos(64, 15);
	wprintf(L"%ls",L"made by HELTA");
}

#define KEY_PRESS(vk) ((GetAsyncKeyState(vk)&1)?1:0)

void Pause()
{
	while (1)
	{
		Sleep(200);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}
_Bool NextIsFood(pSnakeNode pn, pSnake ps)
{
	return (ps->_pFood->x == pn->x && ps->_pFood->y == pn->y);
}
void EatFood(pSnakeNode pn, pSnake ps)
{
	ps->_pFood->next = ps->_pSnake;
	ps->_pSnake = ps->_pFood;
	free(pn);
	pn = NULL;
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->_score += ps->_food_weight;
	CreateFood(ps);
}
void NoFood(pSnakeNode pn, pSnake ps)
{
	pn->next = ps->_pSnake;
	ps->_pSnake = pn;
	pSnakeNode cur = ps->_pSnake;
	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	SetPos(cur->next->x, cur->next->y);
	printf("  ");

	free(cur->next);
	cur->next = NULL;
}
void KillByWall(pSnake ps)
{
	if (ps->_pSnake->x == 0 || ps->_pSnake->x == 56 ||
		ps->_pSnake->y == 0 || ps->_pSnake->y == 26)
	{
		ps->_status = KILL_BY_WALL;
	}
}
void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (cur->x == ps->_pSnake->x && cur->y == ps->_pSnake->y)
		{
			ps->_status = KILL_BY_SELF;
			break;
		}
		cur = cur->next;
	}
}
void SnakeMove(pSnake ps)
{
	pSnakeNode pNewNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNewNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		exit(1);
	}
	switch (ps->_dir)
	{
	case UP:
		pNewNode->x = ps->_pSnake->x;
		pNewNode->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		pNewNode->x = ps->_pSnake->x;
		pNewNode->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		pNewNode->x = ps->_pSnake->x - 2;
		pNewNode->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pNewNode->x = ps->_pSnake->x + 2;
		pNewNode->y = ps->_pSnake->y;
		break;
	}

	if (NextIsFood(pNewNode, ps))
	{
		EatFood(pNewNode, ps);
		pNewNode = NULL;
	}
	else
	{
		NoFood(pNewNode, ps);
	}

	//检测蛇的状态
	KillByWall(ps);
	KillBySelf(ps);
}

void GameRun(pSnake ps)
{
	//打印帮助信息
	PrintHelpInfo();
	do
	{
		//打印总分数和食物的分值
		SetPos(64, 10);
		printf("总分数:%d\n", ps->_score);
		SetPos(64, 11);
		printf("当前食物的分数:%2d\n", ps->_food_weight);
		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->_sleep_time > 80)
			{
				ps->_sleep_time -= 30;
				ps->_food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->_food_weight > 2)
			{
				ps->_sleep_time += 30;
				ps->_food_weight -= 2;
			}
		}
		SnakeMove(ps);
		Sleep(ps->_sleep_time);
	} while (ps->_status == OK);
}

void GameEnd(pSnake ps)
{
	SetPos(24, 12);
	switch (ps->_status)
	{
	case END_NORMAL:
		printf("您主动结束了游戏\n");
		break;
	case KILL_BY_WALL:
		printf("您撞墙了！GG\n");
		break;
	case KILL_BY_SELF:
		printf("您撞到自己了！GG\n");
		break;
	}
	//释放链表

	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
		del = NULL;
	}
}